--
-- @Author:      feilong
-- @DateTime:    2019-12-20 14:02:03
-- @Description: 活动对象

local act_def = require("act_def")
local skynet = require("skynet")
-- local utils = require("utils")

local Activity = class("Activity")

-- 初始化
function Activity:ctor(act_id, conf)
	self.act_id = act_id
	self.service = nil

	self.conf = conf
	self.open_state = conf.open_state
	self.act_type = conf.ac_type
	self.act_content = conf.ac_content --活动内容
	self.act_name = conf.ac_name 

end

function Activity:start()
	local service_name = act_def.service_name_map[self.act_type]
    if not service_name then
        return
    end
    if not self:getOpenState() then 
    	-- dbglog("________活动开关未开启______", self.act_id)
    	return 
    end
    if self.service then 
    	--活动已开启
    	return 
	end
	self.service = skynet.newservice(service_name)
    skynet.call(self.service, "lua", "start", self.act_id)    
    return true
end

function Activity:stop()
    if not self.service then 
    	return 
    end
    skynet.send(self.service, "lua", "stop") 	
    self.service = nil
    return true
end

function Activity:getId()
	return self.act_id
end

function Activity:getService()
	return self.service
end

--活动的开始时间截
function Activity:getBeginTime()
	return string_time(self.conf.ac_begin_time)
end

function Activity:getEndTime()
	return string_time(self.conf.ac_end_time)
end

function Activity:getOpenState()
	return self.open_state
end

--活动类型
function Activity:getType()
	return self.act_type
end

--如果是任务类活动才有任务类型
function Activity:getTaskList()
	if self.act_type == act_def.act_type.TASK then 
		local task_list =  self.act_content.task_list
		local res = {}
		for k, v in pairs(task_list) do 
			table.insert(res, v.task_type)
		end
		return res
	end
	return {}
end

function Activity:getContent()
	return self.act_content
end

function Activity:getName()
	return self.act_name
end

function Activity:isEnable()
	if self:getOpenState() then 
		return true
	end
	return false		
end

--是任务
function Activity:isTask()
	if act:getType() == act_def.act_type.TASK then 
		return true
	end
	return false	
end

function Activity:sendMessage(cmd, ...)
	if not self:getService() then 
		return 
	end
	skynet.send(self:getService(), "lua", cmd, ...)
end
function Activity:callMessage(cmd, ...)
	if not self:getService() then 
		return 
	end
	return skynet.call(self:getService(), "lua", cmd, ...)
end

--返回客户端的数据
function Activity:getData(uid)

	local act_data = {}
	local act_content = self:getContent()
	local act_type = self:getType()
	local activity_common = {
		act_id   = self.act_id,
		act_type = act_type,
		act_begin_time = self:getBeginTime(),
		act_end_time   = self:getEndTime(),
		open_state = self:getOpenState(),
	    bg_url = act_content.bg_url,
	    act_name = self:getName(),
	    act_desc = act_content.ac_desc,
	    ac_class = self.conf.ac_class,
	}
	act_data.activity_common = activity_common
	-- dbglog("get_activity_list===========act_id ",self.act_id, self:isEnable(), self.service)
	if not self:isEnable() then 
		return act_data 
	end
	local defType = act_def.act_type
	--红包雨
	if act_type == defType.RED_PACK then 
		activity_common.show_icon = true --只在大厅显示图标
		local res = self:callMessage("get_state") or {}
		act_data.redpack_activity = res.redpack
	end
	--救援金
	if act_type == defType.RESCUE then		
		activity_common.show_icon = true --只在大厅显示图标
		-- local res = self:callMessage("get_state") or {}
		-- act_data.redpack_activity = res.rescue
	end
	--跳转面板活动
	if act_type == defType.JUMP_PICTURE then
		act_data.jump_module = {
			jump_position = self.act_content.jump_position,
			jump_bt_url = self.act_content.jump_bt_url,
			jump_bt_position = self.act_content.jump_bt_position,				
		}
	end
	--跳转网页活动
	if act_type == defType.JUMP_WEB then
		act_data.jump_web = {
			web_url = self.act_content.web_url,
			jump_bt_url = self.act_content.jump_bt_url,
			jump_bt_position = self.act_content.jump_bt_position,			
		}
	end
	--每日首充反利
	if act_type == defType.SHOU_CHONG  then
		activity_common.show_icon = true --只在大厅显示图标
    	local res = self:callMessage("get_state")
		act_data.day_charge = res or {}
	end

	
	dbglog("_________activity_data___",act_type,"__", defType.RED_PACK, act_data)
	return act_data

end

--更新配置
function Activity:setConf(conf)
	self.conf = conf
	self.open_state = conf.open_state
	self.act_type = conf.ac_type
	self.act_content = conf.ac_content --活动内容
	self.act_name = conf.ac_name 
end


return Activity
